﻿package
{
	import flash.display.InteractiveObject;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;

	public function csTransEvent(target:InteractiveObject,
								 cbEnterFrame:Function = null,
								 cbReleaseOutside:Function = null,
								 cbRelease:Function = null,
								 cbRollOver:Function = null,
								 cbRollOut:Function = null,
								 stopImmediatePropagation:Boolean = true):void
	{
		if(TranslatedMap.MAP[target] != null)
		{
			return;
		}
		TranslatedMap.MAP[target] = 1;

		trace("csTransEvent.csTransEvent at " + target);
		
		target.addEventListener(Event.ENTER_FRAME, onEnterFrame);
		target.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
		target.addEventListener(MouseEvent.ROLL_OVER, onRollOver);
		target.addEventListener(MouseEvent.ROLL_OUT, onRollOut);
			
		function onEnterFrame(e:Event):void
		{
			if(target.stage == null)
			{
				return;
			}
			
			if(stopImmediatePropagation)
			{
				e.stopImmediatePropagation();
			}
			
			try
			{
				if(cbEnterFrame != null)
				{
					cbEnterFrame();
				}
				
				if(target.hasOwnProperty("_onEnterFrame") && target["_onEnterFrame"] != null)
				{
					target["_onEnterFrame"]();
				}
			}
			catch(e:Error)
			{
				trace("csTransEvent.onEnterFrame, target = " + target);
				trace(e.message);
				trace(e.getStackTrace());
			}
		}
		
		function onMouseUp(e:MouseEvent):void
		{
			if(stopImmediatePropagation)
			{
				e.stopImmediatePropagation();
			}
			
			if(target is MovieClip &&
				(target[_attribute.ATTR_MOUSE_STATE_ROLLIN] == null || !target[_attribute.ATTR_MOUSE_STATE_ROLLIN]))
			{
				try
				{
					if(cbReleaseOutside != null)
					{
						cbReleaseOutside();
					}
					
					if(target.hasOwnProperty("_onReleaseOutside") && target["_onReleaseOutside"] != null)
					{
						target["_onReleaseOutside"]();
					}			
				}
				catch(e:Error)
				{
					trace("csTransEvent.onMouseUp.releaseOutside, target = " + target);
					trace(e.message);
					trace(e.getStackTrace());
				}
			}
			else
			{
				try
				{
					if(cbRelease != null)
					{
						cbRelease();
					}
					
					if(target.hasOwnProperty("_onRelease") && target["_onRelease"] != null)
					{
						target["_onRelease"]();
					}
				}
				catch(e:Error)
				{
					trace("csTransEvent.onMouseUp.release, target = " + target);
					trace(e.message);
					trace(e.getStackTrace());
				}
			}
		}
		
		function onRollOver(e:MouseEvent):void
		{
			if(stopImmediatePropagation)
			{
				e.stopImmediatePropagation();
			}
			
			try
			{
				if(cbRollOver != null)
				{
					cbRollOver();
				}
				
				if(target.hasOwnProperty("_onRollOver") && target["_onRollOver"] != null)
				{
					target["_onRollOver"]();
				}
			}
			catch(e:Error)
			{
				trace("csTransEvent.onRollOver, target = " + target);
				trace(e.message);
				trace(e.getStackTrace());
			}
			
			if(target is MovieClip)
			{
				target[_attribute.ATTR_MOUSE_STATE_ROLLIN] = true;
			}
		}
		
		function onRollOut(e:MouseEvent):void
		{
			if(stopImmediatePropagation)
			{
				e.stopImmediatePropagation();
			}
			
			try
			{
				if(cbRollOut != null)
				{
					cbRollOut();
				}
				
				if(target.hasOwnProperty("_onRollOut") && target["_onRollOut"] != null)
				{
					target["_onRollOut"]();
				}
			}
			catch(e:Error)
			{
				trace("csTransEvent.onRollOut, target = " + target);
				trace(e.message);
				trace(e.getStackTrace());
			}
			
			if(target is MovieClip)
			{
				target[_attribute.ATTR_MOUSE_STATE_ROLLIN] = false;
			}
		}
		
	}
	

}

import flash.utils.Dictionary;

class TranslatedMap
{
	public static const MAP:Dictionary = new Dictionary(true);
}